﻿using System;
using System.Runtime.InteropServices;
using EnvDTE;
using Microsoft.VisualStudio.Project.Automation;

namespace RiftAddonStudio.Project.Automation
{
    /// <summary>
    /// Rift project property automation object.
    /// </summary>
    [ComVisible(true)]
    public class OARiftProjectProperty : Property
    {
        private readonly OAProperties parent;
        private readonly string name = String.Empty;

        /// <summary>
        /// Initializes a new instance of the <see cref="OARiftProjectProperty"/> class.
        /// </summary>
        public OARiftProjectProperty()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OARiftProjectProperty"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="name">The name.</param>
        internal OARiftProjectProperty(OARiftProjectProperties parent, string name)
        {
            this.parent = parent;
            this.name = name;
        }

        public object Application
        {
            get { return null; }
        }

        /// <summary>
        /// Gets the Collection containing the Property object supporting this property.
        /// </summary>
        public Properties Collection
        {
            get { return parent; }
        }

        /// <summary>
        /// Gets the top-level extensibility object.
        /// </summary>
        public DTE DTE
        {
            get { return parent.DTE; }
        }

        /// <summary>
        /// Returns one element of a list. 
        /// </summary>
        /// <param name="Index1">The index of the item to display.</param>
        /// <param name="Index2">The index of the item to display. Reserved for future use.</param>
        /// <param name="Index3">The index of the item to display. Reserved for future use.</param>
        /// <param name="Index4">The index of the item to display. Reserved for future use.</param>
        /// <returns>The value of a property</returns>
        // The message is suppressed to follow the csharp naming conventions instead of the base's naming convention that is using c++
        public object get_IndexedValue(object Index1, object Index2, object Index3, object Index4)
        {
            return null;
        }

        /// <summary>
        /// Setter function to set properties values. 
        /// </summary>
        /// <param name="_return"></param>
        public void let_Value(object _return)
        {
            Value = _return;
        }

        /// <summary>
        /// Gets the name of the object.
        /// </summary>
        public string Name
        {
            get { return name; }
        }

        /// <summary>
        /// Gets the number of indices required to access the value.
        /// </summary>
        public short NumIndices
        {
            get { return 0; }
        }

        /// <summary>
        /// Sets or gets the object supporting the Property object.
        /// </summary>
        public object Object
        {
            get { return Value; }
            set { Value = value; }
        }

        /// <summary>
        /// Microsoft Internal Use Only.
        /// </summary>
        /// <value></value>
        /// <returns/>
        public Properties Parent
        {
            get { return parent; }
        }

        /// <summary>
        /// Sets the value of the property at the specified index.
        /// </summary>
        /// <param name="Index1">The index of the item to set.</param>
        /// <param name="Index2">Reserved for future use.</param>
        /// <param name="Index3">Reserved for future use.</param>
        /// <param name="Index4">Reserved for future use.</param>
        /// <param name="Val">The value to set.</param>
        public void set_IndexedValue(object Index1, object Index2, object Index3, object Index4, object Val)
        {
        }

        /// <summary>
        /// Gets or sets the value of the property returned by the Property object.
        /// </summary>
        public object Value
        {
            get { return parent.Target.Node.ProjectMgr.GetProjectProperty(name); }
            set
            {
                if (value is string)
                    parent.Target.Node.ProjectMgr.SetProjectProperty(name, value.ToString());
                else
                    parent.Target.Node.ProjectMgr.SetProjectProperty(name, value.ToString());
            }
        }
    }
}
